using System;
using System.Web.UI;
using SubversionReports.Entities;
using SubversionReports.Utilities;

namespace SubversionReports.Web.Controls
{
    public partial class HistoryView : UserControl
    {
        private int revisionId;
        private string path;

        /// <summary>
        /// Gets or sets the revision id.
        /// </summary>
        /// <value>The revision id.</value>
        public int RevisionId
        {
            get
            {
                if (revisionId.Equals(0))
                {
                    if (Request["RevisionId"] != null)
                        int.TryParse(Request["RevisionId"], out revisionId);
                    else
                        revisionId = SvnWebDav.GetLastPathRevisionId(SvnWebDav.CurrentRepositoryRootUrl, SvnWebDav.CurrentRepositoryUsername, SvnWebDav.CurrentRepositoryPassword, Path);
                }
                return revisionId;
            }
            set { revisionId = value; }
        }

        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>The path.</value>
        public string Path
        {
            get { return Request["Path"] ?? path; }
            set { path = value; }
        }
        public Repository FileLog
        {
            set
            {
                FileRevisionLogRepeater.DataSource = value;
                FileRevisionLogRepeater.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Gets the type of the revision.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <returns></returns>
        protected string GetRevisionType(object o)
        {
            return GetRevisionType(o as Revision);
        }

        /// <summary>
        /// Gets the type of the revision.
        /// </summary>
        /// <param name="revision">The revision.</param>
        /// <returns></returns>
        private string GetRevisionType(Revision revision)
        {
            if (revision != null)
            {
                Path path1 = revision.Paths.Find(delegate(Path p) { return p.RevisionPath.Equals(Path); });
                if (path1 != null)
                    return path1.RevisionType.ToString();
            }
            return string.Empty;
        }
    }
}